<?php

namespace Houdunwang\WeChat\Message;

use Houdunwang\WeChat\WeChat;
use Illuminate\Support\Facades\Log;

/**
 * 被动消息
 * Class Message
 * @package Houdunwang\WeChat\Message
 */
class Message extends WeChat
{
    /**
     * 发送文本消息
     * @param $content
     * @return string
     */
    public function text($content)
    {
        $xml = <<<hd
        <xml>
  <ToUserName><![CDATA[%s]]></ToUserName>
  <FromUserName><![CDATA[%s]]></FromUserName>
  <CreateTime>%s</CreateTime>
  <MsgType><![CDATA[text]]></MsgType>
  <Content><![CDATA[%s]]></Content>
</xml>
hd;
        return sprintf($xml, $this->FromUserName, $this->ToUserName, time(), $content);
    }

    /**
     * 图文消息
     * @param array $news
     * @return string
     */
    public function news(array $news)
    {
        $xml = <<<hd
    <xml>
  <ToUserName><![CDATA[%s]]></ToUserName>
  <FromUserName><![CDATA[%s]]></FromUserName>
  <CreateTime>%s</CreateTime>
  <MsgType><![CDATA[news]]></MsgType>
  <ArticleCount>%s</ArticleCount>
  <Articles>
%s
  </Articles>
</xml>
hd;

        $itemXml = <<<item
  <item>
      <Title><![CDATA[%s]]></Title>
      <Description><![CDATA[%s]]></Description>
      <PicUrl><![CDATA[%s]]></PicUrl>
      <Url><![CDATA[%s]]></Url>
    </item>
item;
        $contents = '';
        foreach ($news as $item) {
            $contents .= sprintf($itemXml, $item['title'], $item['description'], $item['pic'], $item['url']);
        }
        return sprintf($xml, $this->FromUserName, $this->ToUserName, time(), count($news), $contents);


    }

}
